home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / WAREZ 1.1 source Folder / warez ƒ / warez code ƒ / warez file management.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-07  |  4.3 KB  |  180 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        warez file management.c
  4.  
  5. Purpose:    This module handles creating, setting up, and deleting
  6.             temp files, opening and reading the source file, and
  7.             opening and writing the dest file.
  8.  
  9.  
  10. WAREZ -=- nostalgia isn't what it used to be
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "Script.h"
  31. #include "program globals.h"
  32. #include "warez file management.h"
  33. #include "msg environment.h"
  34. #include "util.h"
  35.  
  36. FSSpec            inputFS, outputFS, tempFS;
  37. Boolean            deleteTheThing;
  38. int                inputRefNum, outputRefNum;
  39. unsigned long    gWhatsReallyInInputBuffer;
  40. unsigned long    gTotalOutputLength;
  41.  
  42. void InitFiles(void)
  43. {
  44.     inputRefNum=outputRefNum=0;
  45.     gWhatsReallyInInputBuffer=gTotalOutputLength=0L;
  46. }
  47.  
  48. int OpenInputFile(void)
  49. {
  50.     OSErr            theError;
  51.     
  52.     if (gHasFSSpecs)
  53.         theError=FSpOpenDF(&inputFS, fsRdPerm, &inputRefNum);
  54.     else
  55.         theError=HOpen(inputFS.vRefNum, inputFS.parID, inputFS.name, fsRdPerm, &inputRefNum);
  56.     
  57.     if (theError==noErr)
  58.         GetEOF(inputRefNum, &gInputLength);
  59.     
  60.     return (theError==noErr) ? allsWell : kCantOpenInputFile;
  61. }
  62.  
  63. int CreateTempFile(void)
  64. {
  65.     int                    i;
  66.     Str255                timeStr;
  67.     OSErr                isHuman;
  68.     unsigned long        fileType;
  69.     int                    resultCode;
  70.     
  71.     tempFS=outputFS;
  72.     tempFS.name[0]=0x05;
  73.     tempFS.name[1]='b';
  74.     tempFS.name[2]='o';
  75.     tempFS.name[3]='r';
  76.     tempFS.name[4]='k';
  77.     tempFS.name[5]='.';
  78.     NumToString(Time&0x7fffffff, timeStr);
  79.     for (i=1; i<=timeStr[0]; i++)
  80.         tempFS.name[++(tempFS.name[0])]=timeStr[i];
  81.     
  82.     if (gHasFSSpecs)
  83.         isHuman=FSpCreate(&tempFS, CREATOR, 'TeMp', smSystemScript);
  84.     else
  85.         isHuman=HCreate(tempFS.vRefNum, tempFS.parID, tempFS.name, CREATOR, 'TeMp');
  86.     
  87.     return (isHuman==noErr) ? allsWell : kCantCreateTempFile;
  88. }
  89.  
  90. int SetupTempFile(void)
  91. {
  92.     OSErr                isHuman;
  93.     
  94.     if (gHasFSSpecs)
  95.         isHuman=FSpOpenDF(&tempFS, fsRdWrPerm, &outputRefNum);
  96.     else
  97.         isHuman=HOpen(tempFS.vRefNum, tempFS.parID, tempFS.name, fsRdWrPerm, &outputRefNum);
  98.     
  99.     if (isHuman!=noErr)
  100.         return kCantOpenTempFile;
  101.         
  102.     SetEOF(outputRefNum, 0L);
  103.     SetFPos(outputRefNum, 1, 0L);
  104.  
  105.     return allsWell;
  106. }
  107.  
  108. void FinalizeFiles(Boolean good)
  109. {
  110.     FInfo                theInfo;
  111.     
  112.     if (inputRefNum)
  113.         FSClose(inputRefNum);
  114.     if (outputRefNum)
  115.     {
  116.         SetEOF(outputRefNum, gTotalOutputLength);
  117.         FSClose(outputRefNum);
  118.     }
  119.     FlushVol(0L, outputRefNum);
  120.     
  121.     if (!good)
  122.     {
  123.         if(gHasFSSpecs)
  124.             FSpDelete(&tempFS);
  125.         else
  126.             HDelete(tempFS.vRefNum, tempFS.parID, tempFS.name);
  127.         
  128.         return;
  129.     }
  130.     
  131.     if (deleteTheThing)
  132.     {
  133.         if (gHasFSSpecs)
  134.             FSpDelete(&outputFS);
  135.         else
  136.             HDelete(outputFS.vRefNum, outputFS.parID, outputFS.name);
  137.     }
  138.     FlushVol(0L, outputRefNum);
  139.         
  140.     if (gHasFSSpecs)
  141.         FSpGetFInfo(&inputFS, &theInfo);
  142.     else
  143.         HGetFInfo(inputFS.vRefNum, inputFS.parID, inputFS.name, &theInfo);
  144.  
  145.     theInfo.fdFlags&=~0x0100;    /* clear Inited bit */
  146.     
  147.     if (gHasFSSpecs)
  148.         FSpSetFInfo(&tempFS, &theInfo);
  149.     else
  150.         HSetFInfo(tempFS.vRefNum, tempFS.parID, tempFS.name, &theInfo);
  151.     FlushVol(0L, outputRefNum);
  152.     
  153.     if (gHasFSSpecs)
  154.         FSpRename(&tempFS, outputFS.name);
  155.     else
  156.         HRename(tempFS.vRefNum, tempFS.parID, tempFS.name, outputFS.name);
  157.     FlushVol(0L, outputRefNum);
  158. }
  159.  
  160. int ReadInputFile(int thisFile, unsigned char* buffer, unsigned long count)
  161. {
  162.     OSErr            isHuman;
  163.     unsigned long    temp;
  164.     
  165.     temp=count;
  166.     isHuman=FSRead(thisFile, &temp, buffer);
  167.     return ((isHuman==eofErr) || (isHuman==noErr)) ? allsWell : kCantReadInputFile;
  168. }
  169.  
  170. int WriteTempFile(int thatFile, unsigned char* buffer, unsigned long theLength)
  171. {
  172.     unsigned long    oldFPos;
  173.     
  174.     gTotalOutputLength+=theLength;
  175.     GetFPos(thatFile, &oldFPos);
  176.     SetEOF(thatFile, gTotalOutputLength);
  177.     SetFPos(thatFile, 1, oldFPos);
  178.     return (FSWrite(thatFile, &theLength, buffer)==noErr ? allsWell : kCantWriteTempFile);
  179. }
  180.